package com.mx.testaidldemo

import android.app.Person
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.nfc.Tag
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import kotlinx.android.synthetic.main.activity_main.*
import testview.zhen.com.myapplication.IOneAidlInterface
import testview.zhen.com.myapplication.IPersonBeanCallBack
import testview.zhen.com.myapplication.PersonBean

class MainActivity : AppCompatActivity() {

    companion object{
        val TAG = "MainActivity"
    }

    var isConnection = false
    lateinit var iOne:IOneAidlInterface

    private var servirveConnection = object : ServiceConnection {
        override fun onServiceDisconnected(name: ComponentName?) {
            isConnection =false
        }

        override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
            iOne =  IOneAidlInterface.Stub.asInterface(service)
        }
    }

    private var  istb:IPersonBeanCallBack.Stub =  object : IPersonBeanCallBack.Stub() {
        override fun getName(name: String?) {
                Log.d(TAG,"获取到回调的名字信息name = ${name}")
        }

        override fun getAge(age: Int) {
            Log.d(TAG,"获取到回调的岁数信息age = ${age}")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        button1.setOnClickListener {
            if (isConnection) return@setOnClickListener
            iOne.basis(1,1000*1000,true,1.0f,1.00,"hello world")
        }
        button2.setOnClickListener {
            if (isConnection) return@setOnClickListener
            // 调用Person()方法 获取PersonBean对象
            var person =  iOne.person
            Log.i(TAG,"从服务端获取回来的Person对象信息name = ${person.name},age =${person.age}")
        }
        button3.setOnClickListener {
            if (isConnection) return@setOnClickListener
            //注册回调
            iOne.registerCallback(istb)
            Log.i(TAG,"注册回调")
        }
        button4.setOnClickListener {
            if (isConnection) return@setOnClickListener
            var p = PersonBean().apply {
                name = "同志"
                age = 40
            }
            iOne.setPerson(p)
        }
        button5.setOnClickListener {
            if (isConnection) return@setOnClickListener
            //取消回调
            iOne.unregisterCallback(istb)
            Log.i(TAG,"取消回调")
        }
        button6.setOnClickListener {
            if (isConnection) return@setOnClickListener
        }
        var intent = Intent().also {
            it.setPackage("testview.zhen.com.myapplication")
            it.action = "testview.zhen.com.myapplication.service.aidservice"
        }
        bindService(intent,servirveConnection,Context.BIND_AUTO_CREATE)
    }

    override fun onDestroy() {
        super.onDestroy()
        if (isConnection) unbindService(servirveConnection)
    }

}
